class Session {
	musicOnOff: number = 0;
	soundOnOff: number = 0;

	player = {
		rid: 0,
		rolename: "", //昵称
		agentType: 0,
		logo: "1", //logo  头像图标（1，2，3，4）
		frame: "",  //头像框
		phone: "", //手机号
		sex: 0, //性别  性别 1男 2女
		coin: 0, //金币
		diamond: 0,// 联盟币
		sealCoin: 0,//封印币
		bankpack: 0, //银行保险箱
		vip: 0,      ///vip等级
		exp: 0,//总经验
		unsealCoinQuota: 0,//可解封额度
		vipAdditionPercent: 0, //当前vip加成百分比
		adress: "", //注册地址
		realname: "",
		bank_number: "",//银行卡号 
		alipay_number: "",//支付宝账号

		freeLuckTime: 0, //免费转盘次数
		totalLuckTimes: 0, //转盘总次数
		clubs: [],  //俱乐部列表
		parent_rid: 0,//直属代理上级,
		is_agent: false,  //是否为代理
		have_login_passwd: false,
	};
	curClub = 0; //当前俱乐部
	curClubId = 0; //当前俱乐部ID
	token: string = "";
	id: string = "";
	public config: any = {};
	public TransferState = { CLOSE: 0, OPEN: 1, ONLY_AGENT: 2 };

	/** 聊天数据 (create by tm) */
	chatData: any = null;
	/** 私聊menu数据 (create by tm) */
	pMenu: any[] = null;
	init() {
		console.log("init Session");
		//初始化聊天数据
		this.chatData = {};
		this.pMenu = [];
		//this.curGameId = gameConfig.gameId.GAME_HALL;
		// this.officialWebsite = gameConfig.officialWebsite;

		//this.updatePlayer();

		var s = cc.sys.localStorage.getItem("musicOnOff");
		var music = JSON.parse(s);
		if (music === null) {
			music = 1;
			this.saveMusicOnOff(music);
		}
		this.musicOnOff = music;

		var v = cc.sys.localStorage.getItem("soundOnOff");
		var sound = JSON.parse(v);
		if (sound === null) {
			sound = 1;
			this.saveSoundOnOff(sound);
		}
		this.soundOnOff = sound;

		this.id = "";
	}

	setId(id: string) {
		this.id = id || "";
	}

	getId() {
		return this.id || "";
	}

	saveMusicOnOff(onOrOff) {
		cc.sys.localStorage.setItem("musicOnOff", onOrOff);
	}
	saveSoundOnOff(onOrOff) {
		cc.sys.localStorage.setItem("soundOnOff", onOrOff);
	}

	playMusic(path: string) {
		if (this.musicOnOff === 1) {
			cc.vv.audioUtil.playMusic(path);
		}
	}

	playSound(path: string) {
		if (this.soundOnOff === 1) {
			cc.vv.audioUtil.playSound(path);
		}
	}
	setToken(token: string) {
		this.token = token || "";
	}

	getToken() {
		return this.token;
	}

	setPlayer(data = null) {
		this.player = {};
		this.player.vip = 1;

		this.updatePlayer(data);
	}
	updatePlayer(data = null) {
		// this.player = {};
		for (var key in data) {
			if ("function" != typeof data[key]) {
				this.player[key] = data[key];
			}
		}
		//	this.player.clubs = [12345, 2, 3, 4, 5];
		// this.player.phone = 123;
	}

	setCurClub(club?) {
		this.curClub = club || {};
		//this.setCurClubId(club ? club.Id : 0);
	}
	getCurClub() {
		return this.curClub || {};
	}
	setCurClubId(clubId) {
		if (!this.curClub) {
			this.curClub = { Id: clubId || 0 };
		} else {
			this.curClub.Id = clubId || 0;
		}
		// this.curClubId = clubId || 0;
		// this.curClub.Id = clubId || 0;
	}
	getCurClubId() {
		if (!this.curClub) {
			return 0;
		}
		return this.curClub.Id;
	}
	/**
	 * 获取俱乐部职位
	 * 返回 0: 部长， 1：管理员， 2：普通成员, -1:错误（俱乐部不存在， 不在俱乐部）
	 */
	getClubDuty(rid: number, club) {

		if (!rid || !club) {//参数错误
			return -1;
		}
		if (rid === club.groupLeaderId) {//部长
			return 0;
		}
		var _groupCtrArr = club.groupControllerIdArr;
		if (_groupCtrArr) {
			for (var i = 0; i < _groupCtrArr.length; i++) {
				if (rid === _groupCtrArr[i]) {//管理员
					return 1;
				}
			}
		}

		var _menberArr = club.groupMemberIdArr;
		if (_menberArr) {
			for (var i = 0; i < _menberArr.length; i++) {
				if (rid === _menberArr[i]) {//普通成员
					return 2;
				}
			}
		}

		return -1;

	}

	getPlayer() {
		return this.player;
	}
	getRid() {
		return this.player.rid;
	}
	setLogo(index) {
		this.player.logo = index + "";
	}
	getLogo() {
		return this.player.logo;
	}
	setType(type: number) {
		this.player.agentType = type;
	}
	getType() {
		return this.player.agentType;
	}
	setFrame(index) {
		this.player.frame = index + "";
	}
	getFrame() {
		return this.player.frame;
	}
	setRoleName(value) {
		this.player.rolename = value + '';
	}
	getRoleName() {
		return this.player.rolename;
	}
	setSex(value) {
		this.player.sex = value;
	}
	getSex() {
		return this.player.sex;
	}
	setVip(level) {
		this.player.vip = level || 0
	}
	getVip() {
		return this.player.vip;
	}
	setUnsealCoinQuota(unsealCoinQuota) {
		this.player.unsealCoinQuota = unsealCoinQuota || 0
	}
	getUnsealCoinQuota() {
		return this.player.unsealCoinQuota;
	}
	setVipAdditionPercent(vipAdditionPercent) {
		this.player.vipAdditionPercent = vipAdditionPercent || 0
	}
	getVipAdditionPercent() {
		return this.player.vipAdditionPercent;
	}

	setCoin(coin) {
		this.player.coin = coin || 0;
		console.log("--------updateBaseInfo  coin:", coin);
	}
	getCoin() {
		return this.player.coin;
	}
	setDiamond(diamond) {
		this.player.diamond = diamond || 0;
	}
	getDiamond() {
		return this.player.diamond;
	}

	setSealCoin(sealCoin) {
		this.player.sealCoin = sealCoin || 0
	}
	getSealCoin() {
		return this.player.sealCoin
	}
	setExp(exp) {
		this.player.exp = exp || 0
	}
	getExp() {
		return this.player.exp
	}



	getPhone() {
		return this.player.phone;
	}
	setPhone(phone) {
		this.player.phone = phone
	}
	setBankpack(value) {
		this.player.bankpack = value;
	}

	getBankpack() {
		return this.player.bankpack || 0;
	}

	setIdentity(data) {
		this.player.phone = data.phone;
		this.player.realname = data.real_name || this.player.realname;

		var _account = cc.vv.GLUtils.getCurAccount() || {};
		_account.phone = data.phone;
		_account.rid = this.player.rid;
		_account.logintoken = data.logintoken;
		_account.auth_type = data.auth_type;
		_account.user_name = data.user_name;
		cc.vv.GLUtils.setCurAccount(_account)
		cc.vv.GLUtils.addAccountList(_account);
	}

	setRealName(value) {
		this.player.realname = value;
	}
	getRealName() {
		return this.player.realname;
	}

	//银行卡号 
	setBank_number(number: string) {
		this.player.bank_number = number || "";
	}
	getBank_number() {
		return this.player.bank_number;
	}
	//支付宝账号
	setAlipay_number(number: string) {
		this.player.alipay_number = number || "";
	}
	getAlipay_number() {
		return this.player.alipay_number;
	}

	getClubs() {
		return this.player.clubs;
	}
	setParentRid(rid: number) {
		this.player.parent_rid = rid;
	}
	getParentRid() {
		if(this.player.is_agent){
			return this.player.rid;
		}else {
			return this.player.parent_rid;
		}
	}

	unBind(data) {
		switch (data.unbindType) {
			case "bankcard":
				this.player.bank_number = "";
				break;
			case "alipay":
				this.player.alipay_number = "";
				break;
			default:
				break;
		}
	}

	reset() {
		this.player = {
			rid: 0,
			rolename: "", //昵称
			agentType: 0,
			logo: "1", //logo  头像图标（1，2，3，4）
			frame: "",  //头像框
			phone: "", //手机号
			sex: 0, //性别  性别 1男 2女
			coin: 0, //金币
			diamond: 0,// 联盟币
			sealCoin: 0,//封印币
			bankpack: 0, //银行保险箱
			vip: 0,      ///vip等级
			exp: 0,//总经验
			unsealCoinQuota: 0,//可解封额度
			vipAdditionPercent: 0, //当前vip加成百分比
			adress: "", //注册地址
			realname: "",
			bank_number: "",//银行卡号 
			alipay_number: "",//支付宝账号

			freeLuckTime: 0, //免费转盘次数
			totalLuckTimes: 0, //转盘总次数
			clubs: [],  //俱乐部列表
			parent_rid: 0,//直属代理上级,
			is_agent: false,  //是否为代理
			have_login_passwd: false,
		};
		this.curClub = 0; //当前俱乐部
		this.curClubId = 0; //当前俱乐部ID
		this.token = "";
		this.id = "";
	}

	public setConfig(config: any | string) {
		if (typeof config == 'string') {
			config = JSON.parse(config);
		}
		this.config = config || {};
	}
	public getConfig() {
		return this.config;
	}
	public setConfigTransfer(state: number) {
		if (state == void 0) return false;
		this.config.player_transfer = state;
		return true;
	}
	public getConfigTransfer(): number {
		return this.config.player_transfer;
	}
	public setConfigLuckyPacket(state: number) {
		if (state == void 0) return false;
		this.config.player_redpacket = state;
		return true;
	}
	/**
	 * 0 关 1 开
	 *  */
	public getConfigLuckyPacket(): number {
		return this.config.player_redpacket;
	}

	//获取是否是代理
	getIsAgent(callBack, callFail = null) {
		// cc.vv.GLUtils.sendRequest("GetIdentity", {},  (err, res) => {
		// 		if (err) {
		// 			callFail && callFail();
		// 			cc.vv.GLUtils.showTips(err);
		// 		}else {
		// 			if(res.isAgent == 1 || res.isAgent == 2 ){
		// 				callBack && callBack(true);
		// 			}else {
		// 				callBack && callBack(false);
		// 			}
		// 		}
		// })
		if (this.player.is_agent) {
			callBack && callBack(true);
		} else {
			callBack && callBack(false);
			callFail && callFail(false);
		}
	}

	//获取是否绑定了密码
	getIsBangDingPw() {
		return this.player.have_login_passwd;
	}
	//----------------聊天相关 create by tm-----------------
	/** 添加聊天数据 */
	setChatData(_chatid, _chattype, _chat): void {
		if (!this.chatData) this.chatData = {};
		let _key: string = this.getChatDataKey(_chatid, _chattype);
		if (this.chatData[_key]) {
			this.chatData[_key].push(_chat);
		} else {
			this.chatData[_key] = [_chat];
		}
	}
	/** 获得所有聊天数据 */
	getChatData() {
		return this.chatData;
	}
	/** 获得某一会话聊天数据 */
	getOneChatData(_chatid, _chattype) {
		if (!this.chatData) return null;
		let _key = this.getChatDataKey(_chatid, _chattype);
		if (!this.chatData[_key]) {
			this.chatData[_key] = [];
		}
		return this.chatData[_key];
	}
	/** 移除某条会话 */
	removeOneChatData(_chatid, _chattype): void {
		if (!this.chatData) return;
		delete this.chatData[this.getChatDataKey(_chatid, _chattype)];
	}
	/** 移除聊天数据 */
	removeChatData(): void {
		if (!this.chatData) return;
		for (let _key in this.chatData) {
			delete this.chatData[_key];
		}
		this.chatData = null;
	}
	/** 获得聊天数据的key */
	private getChatDataKey(_chatid, _chattype): string {
		if (_chattype != 3) {
			return `${_chattype}@${_chatid}`;
		} else {
			return `${_chattype}@hall`;
		}
	}
	/** 设置私聊menu */
	setPMenu(_menu): void {
		if (!this.pMenu) this.pMenu = [];
		this.pMenu.push(_menu);
	}
	/** 获得所有私聊menu */
	getAllPMenu() {
		return this.pMenu;
	}
	/** 移除某条私聊menu */
	removePMenu(_chatid, _chattype): void {
		if (!this.pMenu) return;
		let _curIdx: number = -1;
		this.pMenu.forEach((_v, _idx) => {
			if (_v.chatId == _chatid && _v.type == _chattype) {
				_curIdx = _idx;
			}
		});
		_curIdx >= 0 && this.pMenu.splice(_curIdx, 1);
	}
	/** 判断某条私聊menu是否存在 */
	isPMenu(_chatid, _chattype) {
		let _isMenu: boolean = false;
		this.pMenu && this.pMenu.forEach(_v => {
			if (_v.chatId == _chatid && _v.type == _chattype) {
				_isMenu = true;
			}
		});
		return _isMenu;
	}
	/** 表情文本转换 (返回表情文本 如果没有返回值在,则不是表情文本)
	 * @param _emoticon 表情代表的文字
	 * @param _isEmoticon 是不是转换成表情文字,否则转换成表情name
	*/
	txtEmoticon(_emoticon: string, _isEmoticon: boolean = true): string {
		let _key: string = `@emoticon`;
		if (_isEmoticon) {
			return `${_key}_${_emoticon}`;
		} else {
			let _nameArr: string[] = _emoticon.split(`_`);
			if (_nameArr[0] == _key) {
				return _nameArr[1];
			}
			return null;
		}
	}
	/** 获得聊天类型和聊天内容 
	 * @param _content 服务器传过来的消息文本内容
	*/
	getChatContent(_content: string) {
		//聊天内容类型 (1:文本;2:表情;3:语音;)
		let _type: number = 1;
		let _txt: string = _content;
		let _emoticon = this.txtEmoticon(_txt, false);
		//当前内容是表情文字
		if (_emoticon) {
			_type = 2;
			_txt = _emoticon;
		}
		return {
			contentType: _type,
			contentTxt: _txt
		}
	}

	isVip(){
		return this.player.vip;
	}
}

export let session: Session = new Session();
